package com.lzb.browser.dialog;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.lzb.browser.Content;
import com.lzb.browser.R;
import com.lzb.browser.basic.dialog.BaseSmartDialog;
import com.lzb.browser.basic.utils.SPUtil;
import com.lzb.browser.databinding.DialogAddAlbumBinding;
import com.lzb.browser.databinding.DialogMoreBinding;

public class MoreDialog extends BaseSmartDialog<DialogMoreBinding> implements View.OnClickListener {

    public MoreDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_more;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.llNightMode.setOnClickListener(this);
        mDataBinding.llFullScreenMode.setOnClickListener(this);
        mDataBinding.llImagelessMode.setOnClickListener(this);
        mDataBinding.llTracelessMode.setOnClickListener(this);
        mDataBinding.llHistory.setOnClickListener(this);
        mDataBinding.llAddCollect.setOnClickListener(this);
        mDataBinding.llSetting.setOnClickListener(this);
        mDataBinding.llRefresh.setOnClickListener(this);
        mDataBinding.llPicture.setOnClickListener(this);
        mDataBinding.llCacheDownload.setOnClickListener(this);
        mDataBinding.llInPageSarch.setOnClickListener(this);
        mDataBinding.llExit.setOnClickListener(this);

        checkNightMode();
        checkFullScreenMode();
        checkImagelessMode();
        checkTracelessMode();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.llNightMode) {
            dismiss();
            boolean isNightMode = SPUtil.getBoolean(getContext(), Content.isNightMode, false);
            isNightMode = !isNightMode;
            SPUtil.putBoolean(getContext(), Content.isNightMode, isNightMode);

            checkNightMode();

            if (listener != null) {
                listener.clickItem(0);
            }
        } else if (id == R.id.llFullScreenMode) {
            dismiss();
            boolean isFullScreenMode = SPUtil.getBoolean(getContext(), Content.isFullScreenMode, false);
            isFullScreenMode = !isFullScreenMode;
            SPUtil.putBoolean(getContext(), Content.isFullScreenMode, isFullScreenMode);

            checkFullScreenMode();

            if (listener != null) {
                listener.clickItem(1);
            }
        } else if (id == R.id.llImagelessMode) {
            dismiss();
            boolean isImagelessMode = SPUtil.getBoolean(getContext(), Content.isImagelessMode, false);
            isImagelessMode = !isImagelessMode;
            SPUtil.putBoolean(getContext(), Content.isImagelessMode, isImagelessMode);

            checkImagelessMode();

            if (listener != null) {
                listener.clickItem(2);
            }
        } else if (id == R.id.llTracelessMode) {
            dismiss();
            boolean isTracelessMode = SPUtil.getBoolean(getContext(), Content.isTracelessMode, false);
            isTracelessMode = !isTracelessMode;
            SPUtil.putBoolean(getContext(), Content.isTracelessMode, isTracelessMode);

            checkTracelessMode();

            if (listener != null) {
                listener.clickItem(3);
            }
        } else if (id == R.id.llHistory) {
            dismiss();
            if (listener != null) {
                listener.clickItem(4);
            }
        } else if (id == R.id.llAddCollect) {
            dismiss();
            if (listener != null) {
                listener.clickItem(5);
            }
        } else if (id == R.id.llSetting) {
            dismiss();
            if (listener != null) {
                listener.clickItem(6);
            }
        } else if (id == R.id.llRefresh) {
            dismiss();
            if (listener != null) {
                listener.clickItem(7);
            }
        } else if (id == R.id.llPicture) {
            dismiss();
            if (listener != null) {
                listener.clickItem(8);
            }
        } else if (id == R.id.llCacheDownload) {
            dismiss();
            if (listener != null) {
                listener.clickItem(9);
            }
        } else if (id == R.id.llInPageSarch) {
            dismiss();
            if (listener != null) {
                listener.clickItem(10);
            }
        } else if (id == R.id.llExit) {
            dismiss();
            if (listener != null) {
                listener.clickItem(11);
            }
        }
    }

    private void checkNightMode() {
        boolean isNightMode = SPUtil.getBoolean(getContext(), Content.isNightMode, false);

        if (isNightMode) {
            mDataBinding.ivNightMode.setImageResource(R.drawable.yejianmoshi1);
            mDataBinding.tvNightMode.setTextColor(Color.parseColor("#BF96FF"));
        } else {
            mDataBinding.ivNightMode.setImageResource(R.drawable.yejianmoshi);
            mDataBinding.tvNightMode.setTextColor(Color.parseColor("#CCCCCC"));
        }
    }

    private void checkFullScreenMode() {
        boolean isFullScreenMode = SPUtil.getBoolean(getContext(), Content.isFullScreenMode, false);

        if (isFullScreenMode) {
            mDataBinding.ivFullScreenMode.setImageResource(R.drawable.quanpingmoshi1);
            mDataBinding.tvFullScreenMode.setTextColor(Color.parseColor("#BF96FF"));
        } else {
            mDataBinding.ivFullScreenMode.setImageResource(R.drawable.quanpingmoshi);
            mDataBinding.tvFullScreenMode.setTextColor(Color.parseColor("#CCCCCC"));
        }
    }

    private void checkImagelessMode() {
        boolean isImagelessMode = SPUtil.getBoolean(getContext(), Content.isImagelessMode, false);

        if (isImagelessMode) {
            mDataBinding.ivImagelessMode.setImageResource(R.drawable.wutumoshi1);
            mDataBinding.tvImagelessMode.setTextColor(Color.parseColor("#BF96FF"));
        } else {
            mDataBinding.ivImagelessMode.setImageResource(R.drawable.wutumoshi);
            mDataBinding.tvImagelessMode.setTextColor(Color.parseColor("#CCCCCC"));
        }
    }

    private void checkTracelessMode() {
        boolean isTracelessMode = SPUtil.getBoolean(getContext(), Content.isTracelessMode, false);

        if (isTracelessMode) {
            mDataBinding.ivTracelessMode.setImageResource(R.drawable.wuhenmoshi1);
            mDataBinding.tvTracelessMode.setTextColor(Color.parseColor("#BF96FF"));
        } else {
            mDataBinding.ivTracelessMode.setImageResource(R.drawable.wuhenmoshi);
            mDataBinding.tvTracelessMode.setTextColor(Color.parseColor("#CCCCCC"));
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickItem(int type);
    }
}
